package com.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.java.model.*;
import com.java.service.*;
import com.java.utils.CommonUtils;
import com.java.utils.JsonBody;
import com.java.utils.PageList;
import com.java.utils.ResponseUtil;
import com.java.utils.StringUtil;;

@RestController
@RequestMapping("/buying")
public class BuyingController {
	private BuyingService buyingService;

	public BuyingService getBuyingService() {
		return buyingService;
	}

	@Autowired
	public void setBuyingService(BuyingService buyingService) {
		this.buyingService = buyingService;
	}

	@Autowired
	private ClientService clientService;

	@Autowired
	private ThingService thingService;



	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;

	@Autowired
	private ThingTypeService thingtypeService;
	@Autowired
	private ThingType2Service thingtype2Service;

	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("add")
	public JsonBody add(@RequestBody Buying n) {
		if(buyingService.GetCount(n)>0)
			return Util.SetMap("该物品已存在购物车", false);
			if(buyingService.Add(n)>0)
				return Util.SetMap("成功加入购物车", true);
			else
				return Util.SetMap("加入购物车失败", false);
	}

	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("list")
	public JsonBody list(@RequestBody Buying n) {
		List<Buying> ns=buyingService.Get(n);
		for(Buying buying:ns)
		{
			buying.setClient(clientService.GetByID(buying.getClientId()));
			Thing t=thingService.GetByID(buying.getThingId());
			t.setThingtype(thingtypeService.GetByID(t.getThingtypeId()));
			t.setThingtype2(thingtype2Service.GetByID(t.getThingtype2Id()));
			buying.setThing(t);
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("buying", ns);
		map.put("sum", buyingService.GetSum(n));
		return Util.SetData(map);
	}


	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("editnum")
	public JsonBody EditNum(@RequestBody Buying buying) {
		if(buyingService.EditNum(buying)>0)
			return Util.SetMap("操作成功",true);
		else
			return Util.SetMap("操作失败",false);
	}


	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("del")
	public JsonBody del(int id) {
		if(buyingService.Del(id)>0)
			return Util.SetMap("删除成功", true);
		else
			return Util.SetMap("删除失败", false);
	}


}
